/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plures;

import java.io.Serializable;
import java.util.HashMap;
import persistence.FilePersistence;

/**
 *
 * @author Guilherme
 */
public class Core implements Serializable
{
    private HashMap<Integer, Data> dataMap;
    public Integer vLength;
    public static int VALUE_LENGTH;

    public Core(int dataLength)
    {
        this.dataMap = new HashMap<>();
        if(vLength == null)
        {
            VALUE_LENGTH = dataLength;
            this.vLength = dataLength;
        }
    }
    
    public void initValueLength()
    {
        VALUE_LENGTH = this.vLength;
    }

    public Return create(int key, String value, int client)
    {
        if(this.dataMap.containsKey(key)){
            return new Return(null, null, 404);
        }
        Data d = new Data(key, value, client);
        this.dataMap.put(key, d);
        return new Return(d.getValue(client), d.getVersionsVector(), 200);
    }
    
    public Return checkCreate(int key, String value, int client)
    {
        if(this.dataMap.containsKey(key)){
            return new Return(null, null, 404);
        }
        Data d = new Data(key, value, client);
        return new Return(d.fill(value), d.getVersionsVector(), 200);
    }

    public boolean canCreate(int key, String value, int client)
    {
        if (this.dataMap.containsKey(key))
        {
            return false;
        }
        return true;
    }

    public Return read(int key, int client)
    {
        if(!this.dataMap.containsKey(key)){
            return new Return(null, null, 404);
        }
        Data d = this.dataMap.get(key);
        return new Return(d.getValue(client), d.getVersionsVector(), 200);
    }
    
    public Return checkRead(int key, int client)
    {
        if(!this.dataMap.containsKey(key)){
            return new Return(null, null, 404);
        }
        Data d = this.dataMap.get(key);
        return new Return(d.simpleGetValue(client), d.getVersionsVector(), 200);
    }

    public boolean canRead(int key, int client)
    {
        if (!this.dataMap.containsKey(key))
        {
            return false;
        }
        return true;
    }

    public Return update(int key, String value, int client)
    {
        Data d = this.dataMap.get(key);
        if(d == null){
            return new Return(null, null, 404);
        }
        else if(!d.canUpdate(client))
        {
            return new Return(d.simpleGetValue(client), d.getVersionsVector(), 404); 
        }
        d.setValue(value, client);
        return new Return(d.getValue(client), d.getVersionsVector(), 200); 
    }

    public boolean canUpdate(int key, int client)
    {
        Data d = this.dataMap.get(key);
        if (d == null)
        {
            return false;
        } else if (!d.canUpdate(client))
        {
            return false;
        }
        return true;
    }
    
    public Return checkUpdate(int key, String value, int client)
    {
        Data d = this.dataMap.get(key);
        if(d == null){
            return new Return(null, null, 404);
        }
        else if(!d.canUpdate(client))
        {
            return new Return(d.simpleGetValue(client), d.getVersionsVector(), 404); 
        }
        return new Return(d.fill(value), null, 200); 
    }

    public Return delete(int key, int client)
    {
        Data d = this.dataMap.get(key);
        if (d == null)
        {
            return new Return(null, null, 404); 
        }
        else if(!d.canUpdate(client))
        {
            return new Return(d.getValue(client), d.getVersionsVector(), 404); 
        }
        d = this.dataMap.remove(key);
        return new Return(d.getValue(client), d.getVersionsVector(), 200);
    }

    public boolean canDelete(int key, int client)
    {
        Data d = this.dataMap.get(key);
        if (d == null)
        {
            return false;
        } else if (!d.canUpdate(client))
        {
            return false;
        }
        return true;
    }
    
    public Return checkDelete(int key, int client)
    {
        Data d = this.dataMap.get(key);
        if (d == null)
        {
            return new Return(null, null, 404); 
        }
        else if(!d.canUpdate(client))
        {
            return new Return(d.getValue(client), d.getVersionsVector(), 404); 
        }
        return new Return(d.getValue(client), d.getVersionsVector(), 200);
    }

}
